/**
 * 搬运矿物
 */

let baser = require("./baser");

// 返回baser的内部生命函数
module.exports = function () {
    return baser(
        /**
         * 准备函数，装货
         * @param {Creep} creep
         */
        function (creep) {
            // 准备卸货
            if (creep.store.getFreeCapacity() < 5) {
                return 2;
            }
            let container;
            // 有容器
            if (creep.memory.container_id) {
                creep.say('w');
                container = Game.getObjectById(creep.memory.container_id);
                if (creep.withdraw(container, creep.memory.mineral_type) == ERR_NOT_IN_RANGE) {
                    creep.say('m');
                    creep.moveTo(container, { visualizePathStyle: { stroke: 'rgb(18, 230, 28)' } });
                }
                return 1;
            }

            // 获取矿床
            let mineral = creep.pos.findClosestByRange(FIND_MINERALS);
            creep.memory.mineral_type = mineral.mineralType;
            // 获取旁边的容器
            container = mineral.pos.findClosestByRange(FIND_STRUCTURES, {
                filter: (structure) => structure.structureType == STRUCTURE_CONTAINER &&
                    structure.pos.isNearTo(mineral)
            });
            if (container) {
                creep.memory.container_id = container.id;
            }
            return 1;
        },
        /**
         * 工作函数，卸货
         * @param {Creep} creep
         */
        function (creep) {
            // 准备装货
            if (creep.store.getUsedCapacity() < 5) {
                return 1;
            }
            // 卸货
            if (creep.actionStoreOthers(creep.memory.mineral_type)) {
                return 3;
            }
            return 2;
        },
        /**
         * 其他
         * @param {Creep} creep
         */
        function (creep) {
            // 准备装货
            if (!creep.store.getUsedCapacity() < 5) {
                return 1;
            }
            return 1;
        });
}